package com.w3c.pragmatic.config;

import com.w3c.pragmatic.common.properties.RedissonProperties;
import com.w3c.pragmatic.service.IRedissonCustomer;
import com.w3c.pragmatic.service.impl.RedissonCustomerImpl;
import com.w3c.pragmatic.utils.RedissonUtil;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.redisson.config.SingleServerConfig;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

/**
 * redisson自动装配类
 *
 * @author wangdong
 * @since 2021/12/16 18:22
 */
@Configuration
@ConditionalOnClass(Config.class)
@EnableConfigurationProperties(RedissonProperties.class)
public class RedissonConfig {

    @Resource
    private RedissonProperties redissonProperties;

    /**
     * 单机模式自动装配（自动装配依赖配置参数）
     *
     * @return RedissonClient
     */
    @Bean
    @ConditionalOnProperty(name = "redisson.address")
    public RedissonClient redissonSingle() {
        Config config = new Config();
        SingleServerConfig serverConfig =
                config
                        .useSingleServer()
                        .setAddress(redissonProperties.getAddress())
                        .setTimeout(redissonProperties.getTimeout())
                        .setConnectionPoolSize(redissonProperties.getConnectionPoolSize())
                        .setConnectionMinimumIdleSize(redissonProperties.getConnectionMinimumIdleSize());
        serverConfig.setPassword(redissonProperties.getPassword());
        return Redisson.create(config);
    }

    /**
     * 哨兵模式自动装配（自动装配依赖配置参数）
     *
     * @return RedissonClient
     */
    /*@Bean
    @ConditionalOnProperty(name = "redisson.master-name")
    public RedissonClient redissonSentinel() {
        Config config = new Config();
        SentinelServersConfig serverConfig =
                config
                        .useSentinelServers()
                        .addSentinelAddress(redissonProperties.getSentinelAddresses())
                        .setMasterName(redissonProperties.getMasterName())
                        .setTimeout(redissonProperties.getTimeout())
                        .setMasterConnectionPoolSize(redissonProperties.getMasterConnectionPoolSize())
                        .setSlaveConnectionPoolSize(redissonProperties.getSlaveConnectionPoolSize());
        serverConfig.setPassword(redissonProperties.getPassword());
        return Redisson.create(config);
    }*/

    /**
     * 这里创建RedissonCustomer实例并注入到util类中
     *
     * @param redissonSingle 这里使用RedissonClient单例
     * @return RedissonCustomer实例
     */
    @Bean
    public IRedissonCustomer redissonCustomer(RedissonClient redissonSingle) {
        RedissonCustomerImpl redissonCustomer = new RedissonCustomerImpl();
        redissonCustomer.setRedissonClient(redissonSingle);
        RedissonUtil.setLocker(redissonCustomer);
        return redissonCustomer;
    }
}
